package com.fy.module.qu.controller;

import com.fy.common.dto.PageDto;
import com.fy.common.vo.ApiRest;
import com.fy.common.vo.PageResult;
import com.fy.module.qu.dto.QuSaveDto;
import com.fy.module.qu.entity.Qu;
import com.fy.module.qu.service.IQuService;
import com.fy.module.qu.vo.QuResultVo;
import com.fy.module.repo.entity.Repo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 问题题目 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2023-03-21
 */
@RestController
@RequestMapping("/qu")
@CrossOrigin
@Api(description = "题目管理")
public class QuController {

    @Autowired
    private IQuService quService;


    @PostMapping("save")
    @ApiOperation("添加/修改题目")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public ApiRest save(@RequestBody QuSaveDto quSaveDto){
            return quService.add(quSaveDto);
    }

    @PostMapping("page")
    @ApiOperation("分页查询")
    public PageResult<Qu> findpage(@RequestBody PageDto<QuSaveDto> pageDto){

        return quService.findPage(pageDto);
    }

    @GetMapping("delete/{quId}")
    @ApiOperation("删除题目")
    public ApiRest deleteById(@PathVariable String quId){
        return quService.deleteQuById(quId);
    }

    @DeleteMapping("delete")
    @ApiOperation("批量删除题目")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public ApiRest deleteBatch(@RequestBody String[] quIds){
        List<String> qu_Ids = Arrays.asList(quIds);
        boolean flag = quService.removeBatchByIds(qu_Ids);
        if(flag) return ApiRest.success();
        return ApiRest.failure();
    }

    @PostMapping("findOne/{quId}")
    @ApiOperation("修改题目的查询")
    public QuResultVo findOne(@PathVariable String quId){

        return quService.findOne(quId);
    }

}
